//
//  WmcWatermarkListDBManager.swift
//  YPWatermarkCamera
//
//  Created by 赵洋 on 2022/8/30.
//

import UIKit

// MARK: - 扩展WmcWatermarkListDBManager + WmcDBProtocol

extension WmcWatermarkListDBManager: WmcDBProtocol {
    /// 表名
    static var tableName: String = "watermark_list"

    /// 表头数据列
    static var dataType: [WmcDBManager.DataType] {
        return [
            .primaryKey(), // 主键
            .integer(name: "user_id"), // 模版组id
            .varchar(name: "template_group_id"), // 模版组id
            .varchar(name: "watermark_classify_json"), // 模版组Json列表
            .date(name: "save_time"), // 存储时间
        ]
    }
}

// MARK: - [WmcWatermarkListDBManager]

public struct WmcWatermarkListDBManager {
    /// 插入一条水印列表缓存
    static func insertClassifyList(templateGroupId: String,
                                   classifyList: [WmcWatermarkClassifyModel],
                                   resultBlock: YPBlock.OneParam<Bool>? = nil)
    {
        // 用户信息
        var userId: Int = 0
        if let _userId = WmcLoginManager.shared.yupaoAccountModel?.userId, !_userId.isEmpty {
            userId = _userId.yp.int
        }
        // 查找是否有一样的数据
        WmcDBManager.shared.searchCustomData(tableName: tableName,
                                             condition: "where template_group_id = \"\(templateGroupId)\"") { list in
            if list.count >= 1 { // 有相同数据, 删除所有原数据
                list.forEach { dic in
                    if let id = dic["id"] as? Int { // 遍历移除
                        WmcDBManager.shared.deleteData(tableName: tableName, id: id)
                    }
                }
            }
            // 再进行插入
            let date = WmcDateManager.shared.networkDate.value ?? Date()
            let dateValue = (date.timeIntervalSince1970).yp.int

            guard let json = classifyList.toJSONString() else { return }
            // 调用数据库
            WmcDBManager.shared.insertData(tableName: Self.tableName, insertData: [
                .init(column: Self.dataType[1], value: userId),
                .init(column: Self.dataType[2], value: templateGroupId),
                .init(column: Self.dataType[3], value: json),
                .init(column: Self.dataType[4], value: dateValue),
            ], resultBlock: resultBlock)
        }
    }

    /// 搜索一条水印缓存
    static func searchClassifyList(templateGroupKey: String,
                                   resultBlock: @escaping YPBlock.OneParam<[WmcWatermarkClassifyModel]>)
    {
        // 查找是否有一样的数据
        WmcDBManager.shared.searchCustomData(tableName: tableName,
                                             condition: "where template_group_id = \"\(templateGroupKey)\"") { list in
            if let firstData = list.first,
               let jsonStr = firstData["watermark_classify_json"] as? String,
               let list = [WmcWatermarkClassifyModel].deserialize(from: jsonStr)
            {
                var newList: [WmcWatermarkClassifyModel] = []
                for ele in list {
                    if let ele = ele {
                        newList.append(ele)
                    }
                }
                resultBlock(newList)
            } else {
                resultBlock([])
            }
        }
    }
}
